//
//  WBMainViewController.swift
//  weibo
//
//  Created by GOD on 16/9/2.
//  Copyright © 2016年 磊王. All rights reserved.
//

import UIKit

class WBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setUPChildControllers()
        setComposeButton()

        
    }
    /**
     设置横竖屏
        portrait : 竖屏
        landspace :横屏
     设置当前的方向后，当前控制器，及子控制器都会遵守该方向
     如果播放视频，主要是通过modal展现的
     - returns:
     */
    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return .Portrait
    }
    //实现按钮监听方法
    //private 保证方法私有，仅在当前对象被访问
    //@objc 允许这个函数在运行时通过OC的消息被访问
    @objc private func composeStatus(){
        print("撰写微博")
//        let vc = UIViewController()
//        let nac = UINavigationController(rootViewController: vc)
//        presentViewController(nac, animated: true, completion: nil)
        
    
    }
    private lazy var composeButton :UIButton = UIButton.cz_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    
}
//extension类似于OC的分类，在swift中还可以用来
extension WBMainViewController{
    //添加按钮
    private func setComposeButton(){
        tabBar.addSubview(composeButton)
        let count = CGFloat(childViewControllers.count)
        //计算按钮的宽度
        //遮住容错点
        let w = tabBar.bounds.width / count - 1
        //缩进OC里面叫 CGRectInset 正数向内缩进，负数向外缩进
        composeButton.frame = tabBar.bounds.insetBy(dx: 2 * w, dy: 0)
        composeButton.addTarget(self, action:#selector(composeStatus), forControlEvents: .TouchUpInside)
        
    
    }
    
    private func setUPChildControllers(){
        let array : [[String:AnyObject]] = [
            //首页
            ["clsName" : "WBHomeViewController", "title" : "首页", "imageName" : "home", "visitorInfo" : ["imageName" : "", "message" : "关注一些人，回这里看看有什么惊喜"] ],
            //消息页
            ["clsName" : "WBMessageViewController", "title" : "消息", "imageName" : "message", "visitorInfo" : ["imageName" : "visitordiscover_image_message", "message" : "登陆后，别人评论你的微博，发给你的消息，都会在这里得到展示"]],
            
            ["clsName" : "UIViewController"],
            //发现页
            ["clsName" : "WBDiscoverViewController", "title" : "发现", "imageName" : "discover", "visitorInfo" : ["imageName" : "visitordiscover_image_message", "message" : "登陆后，最新，最热微博尽在掌握，不再会和实事，擦肩而过"]],
            //个人信息页
            ["clsName" : "WBProfileViewController", "title" : "我", "imageName" : "profile", "visitorInfo" : ["imageName" : "visitordiscover_image_profile", "message" : "登陆后，你的微博，相册，个人资料会显示在这里，展示给别人"]],
            
        
        ]
        //测试数据格式是否正确
        //(array as NSArray).writeToFile("/Users/leiwang/Desktop/demo.plist", atomically: true)
        //数组转换成json
        let data = try? NSJSONSerialization.dataWithJSONObject(array, options: [.PrettyPrinted])
        data?.writeToFile("/Users/leiwang/Desktop/demo.json", atomically: true)
        ///Extraneous argument label 'dict:' in call
        var arrayM = [UIViewController]()
        for dict in array {
            arrayM.append(controller(dict))
        }
       viewControllers = arrayM
        //print(viewControllers)
    
    }
    
    private func controller(dict:[String : AnyObject]) -> UIViewController{
        //
        guard let clsName  = dict["clsName"] as? String,
            title = dict["title"] as? String,
            imageName = dict["imageName"] as? String,
            cls = NSClassFromString(NSBundle.mainBundle().namespace + "." + clsName) as? WBBaseViewController.Type,
        visitorDict = dict["visitorInfo"] as? [String:String]
            else {
                print("在main的controller中，别瞎找")
                return UIViewController()
        
        }
        //创建视图控制器
        //将cl
        let vc = cls.init()
        
        vc.title = title
        //设置控制器访客信息字典
        vc.visitorInfo = visitorDict
        
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        
        vc.tabBarItem.selectedImage = UIImage(named:"tabbar_" + imageName + "_selected" )?.imageWithRenderingMode(.AlwaysOriginal)
        
        //设置tabBaar 标题字体
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Highlighted)
        print(vc)
        //实例化导航栏控制器的时候，会调用Push方法将rootvc压栈
        let nav = WBNavigationController(rootViewController: vc)
        
        return nav
        
        
        
        
    }


}
